package com.example.designer.ChainOfResponsibility;

import com.example.designer.ChainOfResponsibility.filter.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhaoyunlong
 * @date 2020/12/17
 * @description
 */
public class Main2 {

    public static void main(String[] args) {
        Msg msg = new Msg();
        msg.setMsg("大家好：， <script> 欢迎访问xxx.com, 大家都是996");
    }

    /**
     *  简单责任链处理逻辑
     */
    public void commonDeal(Msg msg){
        //处理msg
        new HTMLFilter().doFilter(msg);
        new SentsFilter().doFilter(msg);
        System.out.println(msg.getMsg());
    }

    /**
     * 通用责任链式处理
     */
    public void CommonChainDeal(Msg msg){
        //处理msg
        List<Filter> filters = new ArrayList<>();
        filters.add(new HTMLFilter());
        filters.add(new SentsFilter());

        filters.forEach(filter -> {
            filter.doFilter(msg);
        });
        System.out.println(msg.getMsg());
    }

    /**
     * 升级式 责任连链式
     */
    public void upgradeChainDeal(Msg msg){
        //处理msg
        FilterChain filterChain = new FilterChain();
        // 链式  编程
        filterChain.add(new HTMLFilter()).add(new SentsFilter());
        filterChain.doFilter(msg);
        System.out.println(msg.getMsg());
    }

    /**
     * 再度 升级式 责任连链式
     *
     * 继承同一个处理类之后  可以实现链式合并 这个挺牛的
     */
    public void againUpgradeChainDeal(Msg msg){
        //处理msg
        UpgradeFilterChain filterChain = new UpgradeFilterChain();
        // 链式  编程
        filterChain.add(new HTMLFilter()).add(new SentsFilter());

        UpgradeFilterChain filterChain2 = new UpgradeFilterChain();
        // 链式  编程
        filterChain.add(new WebFilter());

        // 链式合并
        filterChain.add(filterChain2);

        filterChain.doFilter(msg);


        System.out.println(msg.getMsg());
    }

}


